﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using Newtonsoft.Json;
using DotNet.Common.NHibernateUtil.QueryFilter;

namespace ZLCMS.Web.SystemManage.Basics.Advert
{
    public partial class AdvertList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        protected string _positionCode;
        protected void Page_Load(object sender, EventArgs e)
        {
            ParamKit.Bind("ShopAdvertPosition", ddl_Adsposition, null, null, null, "-1", null);
            //绑定产品状态下拉框
            ParamKit.Bind("YesNo_10", ddl_status, null, new System.Collections.Generic.List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, Request["status"], "-1");

            //初始化DWZ分页控件
            _positionCode = String.IsNullOrEmpty(Request["positionCode"]) ? "" : Request["positionCode"];
            position_Code.Value = (String.IsNullOrEmpty(Request["code"]) ? _positionCode : Request["code"]);
            ddl_Adsposition.SelectedValue = position_Code.Value;

            //填充网格数据
            //BindList(_framePage.PageIndex, _framePage.PageSize,position_Code.Value);
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                QueryFilterList filters = new QueryFilterList();

                if (ddl_status.SelectedValue != "-1")
                {
                    filters.Add(new StringFilter("Status", ddl_status.SelectedValue));
                }

                if (position_Code.Value != "-1")
                {
                    filters.Add(new StringFilter("PositionCode", position_Code.Value));
                }
                if (!string.IsNullOrEmpty(Request["ptitle"]))
                {
                    filters.Add(new StringFilter("Title", Request["ptitle"]));
                }

                string sql = " from ShopAdvertInfo where 1=1 ";
                foreach (DataFilter item in filters)
                {
                    sql += " and " + item.GetSection();
                }


                IQuery query = session.CreateQuery("select count(*) " + sql);
                filters.SetUp(query);

                //设置记录总数
                _framePage = new FrameHelper(this) { RecordCount = Convert.ToInt32(query.UniqueResult()) };

                query = session.CreateQuery(sql + " order by Sort asc");
                filters.SetUp(query);
                grid.DataSource = query.SetFirstResult((_framePage.PageIndex - 1) * _framePage.PageSize)
                    .SetMaxResults(_framePage.PageSize)
                    .List<ShopAdvertInfo>();
                grid.DataBind();
            }


        }
        
    }
}
